<template>
  <div>
    <el-card
      shadow="always"
      style="margin: 36px"
      :body-style="{ padding: '0px' }"
      :page-size="pageSize"
      :total="rows"
      :current-page="pageNo"
      @current-change="currentChangeHandle"
    >
      <template #header>
        <div>
          <h2>{{ $t("operator.sub_account") }}</h2>
        </div></template
      >
      <el-table ref="multipleTable" v-loading="dataListLoading" :data="dataList">
        <el-table-column
          prop="name"
          header-align="center"
          align="center"
          :label="$t('operator.account_name')"
        />
        <el-table-column
          prop="username"
          header-align="center"
          align="center"
          :label="$t('operator.username')"
        >
          <template v-slot="scope">
            <span style="margin-right: 10px">{{ scope.row.username }}</span>
            <el-tooltip
              class="item"
              effect="dark"
              :content="$t('operator.account_administrator')"
              placement="top"
            >
              <i class="el-icon-user-solid" v-if="scope.row.managerFlag === 1"></i>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column
          prop="orgName"
          header-align="center"
          align="center"
          :label="$t('operator.orgName')"
        />
        <el-table-column
          prop="email"
          header-align="center"
          align="center"
          :label="$t('group.operator.email')"
        />
        <el-table-column
          prop="creTime"
          header-align="center"
          align="center"
          :label="$t('group.operator.creTime')"
        >
          <template v-slot="scope">
            <label>{{ formatDate(scope.row.creTime, "YYYY-MM-DD  HH:mm:ss") }}</label>
          </template>
        </el-table-column>
        <el-table-column
          v-if="status === 1 && isAuth('10400202.MANAGE')"
          header-align="center"
          align="center"
          :label="$t('common.operate')"
        >
          <template v-slot="scope">
            <el-tooltip
              class="item"
              effect="dark"
              :content="$t('operator.resetPassBtn')"
              placement="top"
            >
              <el-button
                type="text"
                icon="el-icon-refresh-left"
                @click="reset(scope.row.userId)"
              />
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              :content="$t('operator.updateEmail')"
              placement="top"
            >
              <el-button
                v-if="scope.row.status == 1"
                type="text"
                icon="el-icon-message"
                @click="updateEmail(scope.row)"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <update-email v-if="updateEmailVisible" ref="updateEmail" @close="updateEmailClose" />
  </div>
</template>

<script>
import { formatDate } from "@/utils/formatDate";
import { getaccountList, resetPassword } from "@/api/operator";
import UpdateEmail from "./update-email";
import { isAuth } from "@/utils";

export default {
  components: {
    UpdateEmail,
  },
  data() {
    return {
      operator: null,
      visible: false,
      comfirmLoading: false,
      operatorId: 0,
      operatorType: "",
      status: 2,
      dataForm: {
        operatorName: "",
        appPkgName: "",
        pubKey: "",
        remark: "",
      },
      updateEmailVisible: false,
      dataList: [],
      dataListSelections: [],
      dataListLoading: false,
      pageNo: 1,
      pageSize: 10,
      rows: 0,
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.operator = JSON.parse(localStorage.getItem("operator-info"));
      this.operatorType = this.operator.operatorType;
      this.operatorId = this.operator.operatorId;
      this.status = this.operator.status;
      this.visible = true;
      this.dataList = [];
      this.pageNo = 1;
      this.getDataList();
    },
    cancel() {
      this.visible = false;
      this.$emit("close");
    },
    updateEmail(row) {
      this.updateEmailVisible = true;
      this.$nextTick(() => {
        this.$refs.updateEmail.init(row.userId);
      });
    },
    updateEmailClose() {
      this.updateEmailVisible = false;
      this.getDataList();
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageNo = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      console.log(val);
      this.pageNo = val;
      this.getDataList();
    },
    formatDate(cellValue, fmt) {
      cellValue = new Date(cellValue).valueOf();
      return formatDate(cellValue, fmt);
    },
    getDataList() {
      this.dataListLoading = true;
      getaccountList({ characterCode: "OPERATOR", keyId: this.operatorId })
        .then((data) => {
          console.log(data);
          this.dataListLoading = false;
          this.dataList = data;
        })
        .catch((e) => {
          this.$confirmError(e);
          this.loading = false;
          this.dataListLoading = false;
        });
    },
    handleClose() {
      this.visible = false;
      this.comfirmLoading = false;
    },
    reset(userId) {
      if (this.operatorType === "DEMO") {
        console.log(this.operatorType);
        this.$confirm(this.$t("operator.resetDefaultConfirm"), "", {
          confirmButtonText: this.$t("common.confirm"),
          cancelButtonText: this.$t("common.cancel"),
          type: "warning",
        }).then(() => {
          this.resetPasswordHanle(userId);
        });
      } else {
        this.$confirm(this.$t("operator.resetConfirm"), "", {
          confirmButtonText: this.$t("common.confirm"),
          cancelButtonText: this.$t("common.cancel"),
          type: "warning",
        }).then(() => {
          this.resetPasswordHanle(userId);
        });
      }
    },
    resetPasswordHanle(userId) {
      resetPassword(userId)
        .then((data) => {
          this.$message({
            type: "success",
            message: this.$t("common.success"),
          });
        })
        .catch((err) => {
          this.$confirmError(err);
        });
    },
    isAuth,
  },
};
</script>
